/**
 * controller_deduct.js
 * 分成相关控制器
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-08-29
 */

//课程分成列表
app.controller('DeductList', function($scope, $http, common){
    //变量声明
    $scope.list     = new Array();
    $scope.nItem    = new Object();
    $scope.nPage    = 1;
    $scope.pageHtml     = '';
    $scope.cate     = 1;
    $scope.condition    = {
        keyword     : '',
        date_start  : '',
        date_end    : ''
    }

    // 初始化JS
    $scope.init_js  = function(){
        //标签页钩子设置
        $('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            switch(type){
                case '#list-course' : 
                    $scope.cate     = 1;
                break;
                case '#list-shopcart' : 
                    $scope.cate     = 2;
                break;
                case '#list-vip' : 
                    $scope.cate     = 3;
                break;
                default: return false; break;
            }
            $scope.get_list(1);
        });

        $('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 2
        });

        //回车键监听
        common.listenEnterKey(function(){
            $scope.get_list(1);
        });
    }

    //加载数据列表
    $scope.get_list     = function(page){
        let url     = config.prefix_url + '/Deduct/getList?p='+page;
        let param   = {
            condition   : $scope.condition,
            cate    : $scope.cate
        }

        $http.post(url, param).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.nPage    = page;
            $scope.pageHtml     = info.data.page_html;
            $scope.list     = info.data.list;
            common.startPage($scope.get_list);
        })

        $scope.loading  = true;
    }

    //详情获取
    $scope.get_item   = function(moneyId){
        let url     = config.prefix_url + '/Money/getDetailItem?id='+moneyId;
        $http.get(url).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.nItem    = info.data.info;
            $scope.nDeductList  = info.data.deduct_list;
            $('.modal.get-item').modal('show');
        })
        $scope.loading  = true;
    }

    //导出数据
    $scope.out_excel    = function(){
        let url     = config.prefix_url + '/Deduct/outExcel';
        let param   = {
            condition   : $scope.condition,
            cate    : $scope.cate
        }

        $http.get(url + '?' + param).success(function(info){
            console.log(info);
            $scope.loading     = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            let count   = parseInt(info.data.count);
            param.start     = 0;
            param.length    = 5000;
            while(param.start < count){
                window.open(url + '?' + httpBuildQuery(param), '_blank');
                param.start     = param.start + param.length;
            }
        })

        $scope.loading  = true;
    }

    $scope.get_list(1);
    $scope.init_js();
})